package com.qf.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.aliyuncs.exceptions.ClientException;
import com.qf.constant.MessageConstant;
import com.qf.constant.RedisMessageConstant;
import com.qf.entity.Result;
import com.qf.utils.SMSUtils;
import com.qf.utils.ValidateCodeUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import redis.clients.jedis.JedisPool;

/**
 * @author zhouhao
 * @create 2021-12-24 19:15
 */
@RestController
@RequestMapping("validateCode")
public class ValidateCodeController {

    @Autowired
    JedisPool jedisPool;

    @RequestMapping("send4Order")
    public Result send4Order(String telephone) {
        Integer code = ValidateCodeUtils.generateValidateCode(4);//随机生成4位数的验证码
        try {
            SMSUtils.sendShortMessage(SMSUtils.VALIDATE_CODE, telephone, code.toString());

        } catch (ClientException e) {
            e.printStackTrace();
            new Result(false, MessageConstant.SEND_VALIDATECODE_FAIL);
        }
        System.out.println("发送到手机验证码是:" + code);
        //将生成的验证码缓存到redis,并指定过期时间
        jedisPool.getResource().setex(telephone + RedisMessageConstant.SENDTYPE_ORDER, 5 * 60, code.toString());
        return new Result(true, MessageConstant.SEND_VALIDATECODE_SUCCESS);
    }

    //手机快速登录获取验证码
    @RequestMapping("send4Login")
    public  Result send4Login(String telephone){
        Integer code = ValidateCodeUtils.generateValidateCode(6);//随机生成4位数的验证码
        try {
            SMSUtils.sendShortMessage(SMSUtils.VALIDATE_CODE, telephone, code.toString());
        } catch (ClientException e) {
            e.printStackTrace();
            new Result(false, MessageConstant.SEND_VALIDATECODE_FAIL);
        }
        //将生成的验证码缓存到redis,并指定过期时间
        jedisPool.getResource().setex(telephone + RedisMessageConstant.SENDTYPE_LOGIN, 5 * 60, code.toString());
        return new Result(true, MessageConstant.SEND_VALIDATECODE_SUCCESS);
    }
}
